coefplot = function(model, names, title, y_axis = T){
  nums = which(!model$term %in% c("(Intercept)", "as.factor(treatment)1", "as.factor(treatment)2", "as.factor(treatment)3"))
  modelcoef = model$estimate[nums]
  ylo = model$conf.low[nums]
  yhi = model$conf.high[nums]
  names = names %>% factor(levels = names)
  names = rep(names, 3)
  facet = model$outcome[nums]
  dfplot = data_frame(names, modelcoef, ylo, yhi, facet) %>% 
    mutate(facet = recode(facet, "pca_attitudes" = "Attitudes", "pca_impact" = "Impact", "pca_policy" = "Policy"),
           colours = ifelse(ylo < 0 & yhi > 0, "firebrick4", "steelblue4")) %>%
    arrange(desc(names))
  
  if(y_axis == F){
    p = ggplot(dfplot, aes(x=names, y=modelcoef, ymin=ylo, ymax=yhi, color = "a")) + 
      geom_pointrange() + 
      theme_economist() + theme(axis.text.x = element_text(size = 6), title = element_text(size = 8), 
                                axis.title = element_text(size = 7), strip.text = element_text(size = 8),
                                plot.title = element_text(face = "bold", size = 10), legend.position = "none",
                                axis.text.y = element_text(size = 8)) + 
      geom_hline(yintercept = 0, linetype = 2) + 
      coord_flip(ylim = c(-0.3, 0.3)) + facet_wrap(~facet) + 
      scale_x_discrete(limits = rev(levels(names))) + 
      scale_color_economist() + 
      labs(x = "", y = "Change in Pro-Refugee Views (First PC)", title = title) + 
      theme(axis.text.y = element_blank())
  }else{
    p = ggplot(dfplot, aes(x=names, y=modelcoef, ymin=ylo, ymax=yhi, color = "a")) + 
      geom_pointrange() + 
      theme_economist() + theme(axis.text.x = element_text(size = 6), title = element_text(size = 8), 
                                axis.title = element_text(size = 7), strip.text = element_text(size = 8),
                                plot.title = element_text(face = "bold", size = 10), legend.position = "none",
                                axis.text.y = element_text(size = 8)) + 
      geom_hline(yintercept = 0, linetype = 2) + 
      coord_flip(ylim = c(-0.3, 0.3)) + facet_wrap(~facet) + 
      scale_x_discrete(limits = rev(levels(names))) + 
      scale_color_economist() + 
      labs(x = "", y = "Change in Pro-Refugee Views (First PC)", title = title)
  }
  return(p)
}
